package com.zlt.appparking.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class StringUtil {

	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str);
	}

	/**
	 * 判断字符串不能为空
	 * @param str
	 * @return
	 */
	public static boolean isNotNull(String str) {
		return str != null && !"".equals(str);
	}

	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(new Date());
	}

	private static final String STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	private static final String STRING2 = "0123456789";

	/**
	 * 随机生成指定长度的字符串
	 * @param len
	 * @return
	 */
	public static String randomStr (int len) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			int index = (int) (Math.random() * STRING.length());
			builder.append(STRING.charAt(index));
		}
		return builder.toString();
	}

	/**
	 * 随机生成指定长度的数字
	 * @param len
	 * @return
	 */
	public static String randomStr2 (int len) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < len; i++) {
			int index = (int) (Math.random() * STRING2.length());
			builder.append(STRING2.charAt(index));
		}
		return builder.toString();
	}

	/**
	 * 获取一个uuid字符串
	 * @return
	 */
	public static String uuid (){
		return UUID.randomUUID().toString();
	}

	/**
	 * 将图片加密到base64的字符串
	 * @param bufferedImage
	 * @return
	 */
	public static String changeImageToString(BufferedImage bufferedImage){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			ImageIO.write(bufferedImage,"jpg",bos);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(bos.toByteArray());
	}

	/**
	 * 获取当前时间的周次
	 *  2022-18W
	 * @return
	 */
	public static String getWeeks(){
		Calendar calendar = Calendar.getInstance();
		//获取年份
		int year = calendar.get(Calendar.YEAR);

		//获取周
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		return year + "-" + week + "W";
	}

	/**
	 * 取两次日期的时间间隔，单位为天
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int dayBetween(Date smdate,Date bdate) throws ParseException{
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long day = (time2-time1)/(1000*3600*24);
		return Integer.parseInt(String.valueOf(day));
	}

	/**
	 * 取两次日期的时间间隔，单位为ms--毫秒
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static Long diffBetween(Date smdate,Date bdate) throws ParseException{
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long diff = (time2-time1);
		return Long.parseLong(String.valueOf(diff));
	}

}
